$(function () {
  //个人中心
  $('.member').hover(function () {
    $('.member_ul').show().animate({
      mul:{
        o: 100,
        h: 120
      }
    })
  },function () {
    $('.member_ul').animate({
      mul:{
        h: 0,
        o: 0
      },
      fn: function () {
        $('.member_ul').hide()
      }
    })
  });
  //遮罩画布
  var login_screen = $('#screen');
  //登录框
  var login = $('#login');
  login.center(350,250).resize(function () {
    if(login.css('display') == 'block'){
      login_screen.look();
    }
  });
  $('#login_close').click(function () {
    login_screen.animate({
      attr: 'o',
      target: 0,
      fn: function () {
        login_screen.unlook()
      }
    });
    login.css('display','none')
  });
  $('.login').click(function () {
    login.center(350,250).css('display','block');
    login_screen.look().animate({
      attr: 'o',
      target: 30
    });
  });
  //注册框
  var reg = $('#reg');
  reg.center(600,550).resize(function () {
    if(reg.css('display') == 'block'){
      login_screen.look();
    }
  });
  $('.reg_close').click(function () {
    login_screen.animate({
      attr: 'o',
      target: 0,
      fn: function () {
        login_screen.unlook()
      }
    });
    reg.css('display','none')
  });
  $('.reg').click(function () {
    reg.center(600,550).css('display','block');
    login_screen.look().animate({
      attr: 'o',
      target: 30
    });
  });
  //表单验证
  $('form').form('user').bind('focus',function () {
    $('#reg .infoUser').css('display','block');
    $('#reg .errorUser').css('display','none');
    $('#reg .succUser').css('display','none');
  }).bind('blur',function () {
    if(trim($(this).value()) == ''){
      $('#reg .infoUser').css('display','none');
    }else if(!/[a-zA-Z0-9_]{2,20}/.test(trim($(this).value()))){
      $('#reg .errorUser').css('display','block');
      $('#reg .infoUser').css('display','none');
      $('#reg .succUser').css('display','none');
    }else{
      $('#reg .succUser').css('display','block');
      $('#reg .infoUser').css('display','none');
      $('#reg .errorUser').css('display','none');
    }
  });
  //密码验证
  $('form').form('pass').bind('focus',function () {
    $('#reg .infoPass').css('display','block');
    $('#reg .errorPass').css('display','none');
    $('#reg .succPass').css('display','none');
  }).bind('blur',function () {
    if(trim($(this).value()) == ''){
      $('#reg .infoPass').css('display','none');
    }else{
      if(checkPass(this)){
        $('#reg .infoPass').css('display','none');
        $('#reg .errorPass').css('display','none');
        $('#reg .succPass').css('display','block');
      }else{
        $('#reg .infoPass').css('display','none');
        $('#reg .errorPass').css('display','block');
        $('#reg .succPass').css('display','none');
      }
    }
  });
  //密码强度验证
  $('form').form('pass').bind('keyup',function () {
    checkPass(this);
  });
  //密码验证函数
  function checkPass (_this) {
    var value = trim($(_this).value());
    var value_length = value.length;
    var code_length = 0;
    var flag = false;
    //第一个必须条件的验证6-20为之间
    if(value_length >= 6 && value_length <= 20){
      $('#reg .infoPass .q1').html('●').css('color','green');
    }else{
      $('#reg .infoPass .q1').html('○').css('color','#666');
    }
    //第二个必须条件的验证，字母或数字或非空字符，任意一个即可满足
    if(value_length > 0 && !/\s/.test(value)){
      $('#reg .infoPass .q2').html('●').css('color','green');
    }else{
      $('#reg .infoPass .q2').html('○').css('color','#666');
    }
    //第三个必须条件的验证，大写字母、小写字母、数字、非空字符任意俩种混拼
    if(/[0-9]/.test(value)){
      code_length++;
    }
    if(/[a-z]/.test(value)){
      code_length++;
    }
    if(/[A-Z]/.test(value)){
      code_length++;
    }
    if(/[^0-9a-zA-Z]/.test(value)){
      code_length++;
    }
    if(code_length >= 2){
      $('#reg .infoPass .q3').html('●').css('color','green');
    }else{
      $('#reg .infoPass .q3').html('○').css('color','#666');
    }
    //安全级别
    if(value_length >= 10 && code_length >= 3){
      $('#reg .infoPass .s1').css('color','green');
      $('#reg .infoPass .s2').css('color','green');
      $('#reg .infoPass .s3').css('color','green');
      $('#reg .infoPass .s4').html('高').css('color','green');
    }else if(value_length >= 8 && code_length >= 2){
      $('#reg .infoPass .s1').css('color','#f60');
      $('#reg .infoPass .s2').css('color','#f60');
      $('#reg .infoPass .s3').css('color','#ccc');
      $('#reg .infoPass .s4').html('中').css('color','#f60');
    }else if(value_length >= 1){
      $('#reg .infoPass .s1').css('color','maroon');
      $('#reg .infoPass .s2').css('color','#ccc');
      $('#reg .infoPass .s3').css('color','#ccc');
      $('#reg .infoPass .s4').html('低').css('color','#f60');
    }else{
      $('#reg .infoPass .s1').css('color','#ccc');
      $('#reg .infoPass .s2').css('color','#ccc');
      $('#reg .infoPass .s3').css('color','#ccc');
      $('#reg .infoPass .s4').html(' ')
    }
    if(value_length >= 6 && value_length <= 20 && !/\s/.test(value) && code_length >= 2) flag = true;
    return flag;
  }
  //拖拽
  reg.drag($('#reg h2').first());

  //百度分享初始化位置
  $('#share').css('top',getScroll().top + (getInner().height - parseInt(getStyle($('#share').first(),'height'))) / 2 + 'px');

  /*addEvent(window,'scroll',function () {
    $('#share').animate({
      attr: 'y',
      target: getScroll().top + (getInner().height - parseInt(getStyle($('#share').first(),'height'))) / 2
    });
  });*/
  setTimeout(function () {
    $(window).bind('scroll',function () {
      $('#share').animate({
        attr: 'y',
        target: getScroll().top + (getInner().height - parseInt(getStyle($('#share').first(),'height'))) / 2
      });
    });
  },100);
  //百度分享收缩效果
  $('#share').hover(function () {
    $(this).animate({
      attr: 'x',
      target: 0
    })
  },function () {
    $(this).animate({
      attr: 'x',
      target: -211
    })
  });
  $('#nav .about li').hover(function(){
    var target = $(this).first().offsetLeft;
    $('#nav .nav_bg').animate({
      attr: 'x',
      target: target + 32,
      t: 30,
      step: 10,
      fn: function () {
        $('#nav .white').animate({
          attr: 'x',
          target: -target + -12
        });
      }
    })
  },function(){
    $('#nav .nav_bg').animate({
      attr: 'x',
      target: 32,
      t: 30,
      step: 10,
      fn: function () {
        $('#nav .white').animate({
          attr: 'x',
          target: -12
        });
      }
    })
  })
  //左侧菜单
  $('#sidebar h2').toggle(function () {
    $(this).next().animate({
      mul: {
        h: 0,
        o: 0
      }
    })
  },function () {
    $(this).next().animate({
      mul: {
        h: 150,
        o: 100
      }
    })
  })
  //轮播器初始化
  $('#banner img').opacity(0);
  $('#banner img').eq(0).opacity(100);
  $('#banner ul li').eq(0).css('color','#333');
  $('#banner strong').html($('#banner img').eq(0).attr('alt'));
  //轮播器计数器
  var banner_index = 1;
  //轮播器种类
  var banner_type = 2;  //1表示透明度，2表示上下滚动
  //自动轮播器
  var banner_timer = setInterval(banner_fn,2000);
  //手动轮播器
  $('#banner ul li').hover(function () {
    clearInterval(banner_timer);
    if($(this).css('color') != 'rgb(51,51,51)' && $(this).css('color') != '#333'){
      banner(this,banner_index==0?$('#banner ul li').elements.length-1:banner_index-1);
    }
  },function () {
    banner_index = $(this).index() + 1;
    banner_timer = setInterval(banner_fn,2000);
  });
  function banner (obj,prev) {
    $('#banner ul li').css('color','#999');
    $(obj).css('color','#333');
    $('#banner strong').html($('#banner img').eq($(obj).index()).attr('alt'));
    if(banner_type == 1){
      $('#banner img').eq(prev).animate({
        attr: 'o',
        target: 0,
        t: 30,
        step: 10
      }).css('zIndex',1);
      $('#banner img').eq($(obj).index()).animate({
        attr: 'o',
        target: 100,
        t: 30,
        step: 10
      }).css('zIndex',2);
    }else if(banner_type == 2){
      $('#banner img').eq(prev).animate({
        attr: 'y',
        target: 150,
        t: 30,
        step: 10
      }).css('zIndex',1).opacity(100);
      $('#banner img').eq($(obj).index()).animate({
        attr: 'y',
        target: 0,
        t: 30,
        step: 10
      }).css('top','-150px').css('zIndex',2).opacity(100);
    }
  };
  function banner_fn () {
    if(banner_index >= $('#banner ul li').elements.length) banner_index = 0;
    banner($('#banner ul li').eq(banner_index).first(),banner_index==0?$('#banner ul li').elements.length-1:banner_index-1);
    banner_index++;
  };
  //当图片进入到可视区域的时候，将图片的xsrc替换到src
  var wait_load = $('.wait_load');
  wait_load.opacity(0);
  $(window).bind('scroll',waitLoad);
  $(window).bind('resize',waitLoad);
  function waitLoad () {
    setTimeout(function () {
      for(var i = 0; i < wait_load.elements.length; i++){
        var _this = wait_load.ge(i);
        if(getInner().height + getScroll().top >= offsetTop(_this)){
          $(_this).attr('src',$(_this).attr('xsrc')).animate({
            attr: 'o',
            target: 100,
            t: 30,
            step: 10
          })
        }
      }
    },100);
  }
  //图片预加载
  var photo_big = $('#photo_big');
  photo_big.center(620,511).resize(function () {
    if(photo_big.css('display') == 'block'){
      login_screen.look();
    }
  });
  $('#photo_big .reg_close').click(function () {
    login_screen.animate({
      attr: 'o',
      target: 0,
      fn: function () {
        login_screen.unlook()
      }
    });
    photo_big.css('display','none');
    $('#photo_big .big img').attr('src','../images/big_loading.gif');
  });
  $('#photo .wait_load').click(function () {
    photo_big.center(620,511).css('display','block');
    login_screen.look().animate({
      attr: 'o',
      target: 30
    });
    //创建一个临时的图片对象，用以保存图片
    var temp_img = new Image(); //创建一个临时区域的图片对象
    $(temp_img).bind('load',function () {
      $("#photo_big .big img").attr('src',temp_img.src).animate({
        attr: 'o',
        target: 100,
        t: 30,
        step: 10
      }).opacity(0)
    })
    temp_img.src = $(this).attr('bigsrc');
    var children = this.parentNode.parentNode;
    prev_next_img(children);
  });
  //拖拽
  photo_big.drag($('#photo_big h2').first());
  //创建一个临时的图片对象，用以保存图片
 /* var temp_img = new Image(); //创建一个临时区域的图片对象
  $(temp_img).bind('load',function () {
    $("#photo_big .big img").attr('src',temp_img.src).animate({
      attr: 'o',
      target: 100,
      t: 30,
      step: 10
    }).opacity(0)
  })*/
  //src属性可以在后台加载这张图片到本地缓存,IE必须把src这个属性放在load事件的下面才有效
  //temp_img.src = 'lujing.jpg';

  //图片鼠标滑过
  $('#photo_big .big .left').hover(function () {
    $('#photo_big .big .sl').animate({
      attr: 'o',
      target: 50,
      t: 30,
      step: 10
    });
  },function () {
    $('#photo_big .big .sl').animate({
      attr: 'o',
      target: 0,
      t: 30,
      step: 10
    });
  });
  $('#photo_big .big .right').hover(function () {
    $('#photo_big .big .sr').animate({
      attr: 'o',
      target: 50,
      t: 30,
      step: 10
    });
  },function () {
    $('#photo_big .big .sr').animate({
      attr: 'o',
      target: 0,
      t: 30,
      step: 10
    });
  });
  //图片上一张
  $('#photo_big .big .left').click(function () {
    $('#photo_big .big img').attr('src','../images/big_loading.gif');
    var current_img = new Image();
    $(current_img).bind('load',function () {
      $('#photo_big .big img').attr('src',current_img.src).animate({
        attr: 'o',
        target: 100,
        t: 30,
        step: 10
      }).opacity(0);
    });
    current_img.src = $(this).attr('src');
    var children = $('#photo dl dt img').ge(prevIndex($('#photo_big .big img').attr('index'),$('#photo').first())).parentNode.parentNode;
    prev_next_img(children);
  });
  //图片下一张
  $('#photo_big .big .right').click(function () {
    $('#photo_big .big img').attr('src','../images/big_loading.gif');
    var current_img = new Image();
    $(current_img).bind('load',function () {
      $('#photo_big .big img').attr('src',current_img.src).animate({
        attr: 'o',
        target: 100,
        t: 30,
        step: 10
      }).opacity(0);
    });
    current_img.src = $(this).attr('src');
    var children = $('#photo dl dt img').ge(nextIndex($('#photo_big .big img').attr('index'),$('#photo').first())).parentNode.parentNode;
    prev_next_img(children);
  });
  function prev_next_img (children) {
    var prev = prevIndex($(children).index(),children.parentNode);
    var next = nextIndex($(children).index(),children.parentNode);
    var prev_img = new Image();
    var next_img = new Image();
    prev_img.src = $('#photo dl dt img').eq(prev).attr('bigsrc');
    next_img.src = $('#photo dl dt img').eq(next).attr('bigsrc');
    $('#photo_big .big .left').attr('src',prev_img.src);
    $('#photo_big .big .right').attr('src',next_img.src);
    $('#photo_big .big img').attr('index',$(children).index());
    $('#photo_big .big .index').html(parseInt($(children).index()) + 1 + '/' + $('#photo dl dt img').elements.length)
  }
});

